import { defineStore } from "pinia";
import {
  store,
  router,
  resetRouter,
  routerArrays,
  storageLocal
} from "../utils";
import { getUserInfo } from "@/api/user";
import { useMultiTagsStoreHook } from "./multiTags";
import { setToken, removeToken, userKey } from "@/utils/auth";

export const useUserStore = defineStore({
  id: "user",
  state: () => ({
    id: storageLocal().getItem(userKey)?.id ?? "",
    // 头像
    avatar: storageLocal().getItem(userKey)?.avatar ?? "",
    // 用户名
    username: storageLocal().getItem(userKey)?.username ?? "",
    // 昵称
    nickname: storageLocal().getItem(userKey)?.nickname ?? "",
    // 页面级别权限
    roles: storageLocal().getItem(userKey)?.roles ?? ["admin"],
    //权限名称
    roleName: storageLocal().getItem(userKey)?.roleName ?? "",
    // 手机号
    phone: storageLocal().getItem(userKey)?.phone ?? "",
    // 邮箱
    email: storageLocal().getItem(userKey)?.email ?? "",
    // vip名称
    vipname: storageLocal().getItem(userKey)?.vipname ?? "",
    // vip开始时间
    vipStartTime: storageLocal().getItem(userKey)?.vipStartTime ?? "",
    // vip结束时间
    vipEndTime: storageLocal().getItem(userKey)?.vipEndTime ?? "",
    // 储存空间
    quantity: storageLocal().getItem(userKey)?.quantity ?? "",
    // 已使用空间
    quantityUsed: storageLocal().getItem(userKey)?.quantityUsed ?? ""
  }),
  actions: {
    /** 存储头像 */
    SET_AVATAR(avatar) {
      this.avatar = avatar;
    },
    /** 存储用户名 */
    SET_USERNAME(username) {
      this.username = username;
    },
    /** 存储昵称 */
    SET_NICKNAME(nickname) {
      this.nickname = nickname;
    },
    /** 存储角色 */
    SET_ROLES(roles) {
      this.roles = roles;
    },
    SET_ID(id) {
      this.id = id;
    },
    SET_PHONE(phone) {
      this.phone = phone;
    },
    SET_EMAIL(email) {
      this.email = email;
    },
    SET_VIPNAME(vipname) {
      this.vipname = vipname;
    },
    SET_VIPENDTIME(vipEndTime) {
      this.vipEndTime = vipEndTime;
    },
    SET_VIPSTARTTIME(vipStartTime) {
      this.vipStartTime = vipStartTime;
    },
    SET_QUANTITUY(quantity) {
      this.quantity = quantity;
    },
    SET_QUANTITYUSED(quantityUsed) {
      this.quantityUsed = quantityUsed;
    },
    SET_ROLENAME(roleName) {
      this.roleName = roleName;
    },
    /** 存储是否勾选了登录页的免登录 */
    SET_ISREMEMBERED() {
      this.isRemembered = True;
    },
    /** 设置登录页的免登录存储几天 */
    SET_LOGINDAY() {
      this.loginDay = 7;
    },
    /** 登入 */
    async login(token) {
      return new Promise((resolve, reject) => {
        getUserInfo()
          .then(res => {
            let data = Object.assign({}, res.data);
            data.accessToken = token;
            data.refreshToken = token;
            data.roles = [res.data.roleTag];
            data.roleName = res.data.roleName;
            data.avatar = res.data.headimgurl;
            if (res.code == 20000) {
              setToken(data);
            }
            resolve(res);
          })
          .catch(error => {
            reject(error);
          });
      });
    },
    /** 前端登出（不调用接口） */
    logOut() {
      this.username = "";
      this.roles = [];
      removeToken();
      useMultiTagsStoreHook().handleTags("equal", [...routerArrays]);
      resetRouter();
      router.push("/login");
    }
    // /** 刷新`token` */
    // async handRefreshToken(data) {
    //   return new Promise((resolve, reject) => {
    //     refreshTokenApi(data)
    //       .then(data => {
    //         if (data) {
    //           setToken(data.data);
    //           resolve(data);
    //         }
    //       })
    //       .catch(error => {
    //         reject(error);
    //       });
    //   });
    // }
  }
});

export function useUserStoreHook() {
  return useUserStore(store);
}
